using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

using uam_fps_game.Utils;
using uam_fps_game.Views;
using uam_fps_game.Settings;

namespace uam_fps_game
{
    public class ShooterGame : Microsoft.Xna.Framework.Game
    {
        public GraphicsDeviceManager GraphicsDeviceMgr { get; protected set; }

        ViewManager mViewManager;

        FpsCounter mFpsCounter;

        KeyboardState mKeyboardState;
        SpriteBatch spriteBatch;

        Console.ConsoleBar mConsole;
        Console.ConsoleBox mConsoleBox;


        SpriteFont spriteFont;

        public ShooterGame()
        {
            GraphicsDeviceMgr = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        protected override void Initialize()
        {
            GraphicsDeviceMgr.PreferredBackBufferWidth = VideoSettings.Instance.ScreenWidth;
            GraphicsDeviceMgr.PreferredBackBufferHeight = VideoSettings.Instance.ScreenHeight;
            GraphicsDeviceMgr.IsFullScreen = false;
            GraphicsDeviceMgr.SynchronizeWithVerticalRetrace = false;
            GraphicsDeviceMgr.PreferMultiSampling = false;
            GraphicsDeviceMgr.ApplyChanges();
            IsFixedTimeStep = false;

            Window.Title = "Shooter";

            base.Initialize();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            mFpsCounter = new FpsCounter(GraphicsDevice, Content);
            mViewManager = new ViewManager(GraphicsDevice, Services);

            View gameView = new GameView();
            mViewManager.AddView(gameView);
            mViewManager.SetActiveView(gameView);

            spriteFont = Content.Load<SpriteFont>("Fonts//SpriteFont1");

            mConsole = new Console.ConsoleBar(this, Content.Load<Texture2D>("Textures//consoleBackground"), GraphicsDevice.PresentationParameters.BackBufferWidth);
            mConsoleBox = new Console.ConsoleBox(this, new Vector2(0, 600));

            //mClientManager.Connect("localhost", 14242);

        }

        protected override void UnloadContent()
        {

        }

        protected override void Update(GameTime gameTime)
        {
            mKeyboardState = Keyboard.GetState();
            if (mKeyboardState.IsKeyDown(Keys.Escape))
                this.Exit();

            Network.ServerManager.Update();
            Network.ClientManager.Update();
            if (Network.ClientManager.mes != "")
                mConsoleBox.AddText(Network.ClientManager.mes);

            mFpsCounter.Update(gameTime);

            mViewManager.Update((float)gameTime.ElapsedGameTime.TotalSeconds);
            mConsole.Update(gameTime, mKeyboardState);
            mConsoleBox.Update(gameTime);

            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            mViewManager.Draw();

            mFpsCounter.Draw();

            mConsole.Draw(gameTime, spriteBatch, spriteFont);
            mConsoleBox.Draw(gameTime, spriteBatch, spriteFont);
            base.Draw(gameTime);
        }
    }
}
